//var baseUrl = 'http://192.168.110.108:8080';
var baseUrl = 'http://localhost:9001';
var loginUser = JSON.parse(sessionStorage.getItem('member'));
var pageInfo = {
	page: 1,
	limit: 100,
};

// 预约动作
$('.list').on('click', '.btn-reserve', function(){
	var id = $(this).attr('data-id');
	console.log(id);
	var token = sessionStorage.getItem('token')
	if (!loginUser || !token) {
		showError('请先登录');
		return;
	}
	$.ajax({
		url: baseUrl + '/fore/reserve',
		data: JSON.stringify({courseId: id}),
		type: 'post',
		dataType: 'JSON',
		contentType: 'application/json; charset=UTF-8',
		beforeSend: function(request) {
			request.setRequestHeader("token", token);
		},
		success(result){
			if (result.code == 200) {
				//
				new bDialog().open({
					html: '预约成功！',
				});
			} else {
				new bDialog().open({
					title: '警告',
					html: result.msg,
				});
			}
		},
		error(xhr,status,error){
			console.log(xhr)
			if (xhr.status == 401) {
				new bDialog().open({
					title: '警告',
					html: xhr.responseJSON.msg,
				});
			} else {
				new bDialog().open({
					title: '警告',
					html: '请求失败！',
				});
			}
		}
	});

});
// 评分动作
$('.list').on('click', '.btn-reserve2', function() {
	var id = $(this).attr('data-id');
	var token = sessionStorage.getItem('token');

	// 直接使用浏览器弹窗输入分数
	var score = prompt("请输入评分(1-5分):", "5");

	// 验证输入
	if (score !== null) {
		score = parseFloat(score);
		if (isNaN(score) || score < 1 || score > 5) {
			alert("请输入1-5之间的有效数字！");
			return;
		}

		// 发送评分请求
		$.ajax({
			url: baseUrl + '/fore/reserve2',
			data: JSON.stringify({
				courseId: id,
				score: score
			}),
			type: 'post',
			dataType: 'JSON',
			contentType: 'application/json; charset=UTF-8',
			beforeSend: function(request) {
				request.setRequestHeader("token", token);
			},
			success: function(result) {
				if (result.code == 200) {
					alert("评分成功！");
					location.reload(); // 刷新页面
				} else {
					alert(result.msg || "评分失败");
				}
			},
			error: function(xhr) {
				var msg = xhr.status == 401 ?
					xhr.responseJSON.msg : "请求失败！";
				alert(msg);
			}
		});
	}
});
// 取消预约
$('.list').on('click', '.btn-reserve1', function() {
	var id = $(this).attr('data-id');
	console.log(id);
	var token = sessionStorage.getItem('token')

	$.ajax({
		url: baseUrl + '/fore/reserve1/' + id,
		type: 'put',
		data: JSON.stringify({
			memberId: loginUser.id, // 添加用户ID
			courseId: id      // 课程ID
		}),
		dataType: 'JSON',
		contentType: 'application/json; charset=UTF-8',
		beforeSend: function (request) {
			request.setRequestHeader("token", token);
		},
		success(result){
			if (result.code == 200) {
				//
				new bDialog().open({
					html: '取消预约成功！',
				});
			} else {
				new bDialog().open({
					title: '警告',
					html: result.msg,
				});
			}
		},
		error(xhr,status,error){
			console.log(xhr)
			if (xhr.status == 401) {
				new bDialog().open({
					title: '警告',
					html: xhr.responseJSON.msg,
				});
			} else {
				new bDialog().open({
					title: '警告',
					html: '请求失败！',
				});
			}
		}
	});
})

$('.order li').click(function(){
	//
	$('.order li').removeClass('active');
	$(this).addClass("active");

	var order = $(this).attr('data-order');

});

loadCourse();

// 列表模板
var template = '\
    <li>\
        <div class="info">\
            <div class="photo-container">\
                <img class="photo" src="">\
            </div>\
        </div>\
        <div class="reserve">\
            <div class="course">瑜伽</div>\
            <div class="coach">小小</div>\
            <div class="time">2024-05-31 18:30:00</div>\
            <div class="actions">\
                <a class="btn-reserve">立即预约</a>\
                <a class="btn-reserve1">取消预约</a>\
                <a class="btn-reserve2">评分</a>\
            </div>\
        </div>\
    </li>';

// 加载课程
function loadCourse(){
	// ajax
	$.ajax({
		url: baseUrl + '/fore/course',
		data: pageInfo,
		dataType: 'json',
		success(result){
			$('.list').empty();
			$.each(result.data.list, function(index, item){
				var dom = $(template);
				dom.find('.photo').attr('src', item.photo);
				dom.find('.course').text(item.name);
				dom.find('.coach').text(item.cname);
				dom.find('.time').text(item.time.substring(5, 16));
				dom.find('.btn-reserve').attr('data-id', item.id);
				dom.find('.btn-reserve1').attr('data-id', item.id);
				dom.find('.btn-reserve2').attr('data-id', item.id);
				$('.list').append(dom);
			});

		}
	});
}
